package com.qfedu.web;

import com.qfedu.model.Notice;
import com.qfedu.service.NoticeService;
import com.qfedu.service.impl.NoticeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author lenovo
 * @version 1.0
 * @date 2023/4/18 10:45
 * @poject bloo
 */
@WebServlet("/notice")
public class NoticeServlet extends BaseServlet {
    private NoticeService noticeService=new NoticeServiceImpl();
    /**
     * 完成查询
     */
    public void findAll(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        List<Notice> list=noticeService.findAllNotice();
        resp.setContentType("text/html;charset=utf-8");
        req.setAttribute("noticeList",list);
        System.out.println(list);
        req.getRequestDispatcher("/pages/notices/success.jsp").forward(req,resp);
        // resp.sendRedirect("/list.jsp");
    }
    /**
     * 添加公告
     */
    public void addNotice(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
        Notice notice = new Notice();
        String creatdUser = req.getParameter("cretedUser");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        notice.setContent(content);
        notice.setTitle(title);
        notice.setCretedUser(creatdUser);
        System.out.println(notice);
        NoticeServiceImpl noticeService = new NoticeServiceImpl();
        int row = noticeService.addNotice(notice);
        if (row>0){
            //添加成功
            req.getRequestDispatcher("/notice?action=findAll").forward(req,resp);
        }else {
            //添加失败 跳转到错误页面
            req.setAttribute("message","添加失败，请重新操作");
            req.getRequestDispatcher("/pages/notices/error.jsp").forward(req,resp);
        }
    }
    /**
     * 删除公告
     */
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int id = Integer.parseInt(req.getParameter("id"));
        NoticeServiceImpl noticeService = new NoticeServiceImpl();
        int row = noticeService.deleteNotice(id);
        if (row > 0){
            //修改成功
            req.getRequestDispatcher("/notice?action=findAll").forward(req,resp);
        }else {
            //修改失败 跳转到错误页面
            req.setAttribute("message","删除失败，请重新操作");
            req.getRequestDispatcher("/pages/notices/error.jsp").forward(req,resp);
        }
    }
    /**
     * 修改公告
     */
    public void updateNotice(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            Notice notice = new Notice();
            BeanUtils.populate(notice,parameterMap);
            System.out.println(notice);
            NoticeServiceImpl noticeService = new NoticeServiceImpl();
            int row = noticeService.updateNotice(notice);
            if (row > 0){
                req.getRequestDispatcher("/notice?action=findAll").forward(req,resp);
            }else {
                req.setAttribute("message","修改失败，请重新操作");
                req.getRequestDispatcher("/pages/notices/error.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据id查询
    public void findNoticeById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取参数
        String parameter = req.getParameter("id");
        int id = Integer.parseInt(parameter);
        System.out.println(id);
        NoticeServiceImpl noticeService = new NoticeServiceImpl();
        Notice notice = noticeService.findNoticeById(id);
        if (notice!=null){
            //查询成功
            req.setAttribute("notice",notice);
            req.getRequestDispatcher("/page/update.jsp").forward(req,resp);
        }else {
            //查询失败 跳转到错误页面
            req.setAttribute("message","查询失败，请重新操作");
            req.getRequestDispatcher("/pages/notices/error.jsp").forward(req,resp);
        }
    }
}
